﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Collision;
using FarseerPhysics.Common;
using MapLibrary.Players;

namespace MapLibrary
{
    public class WorldViewport
    {
        private float mX;
        private float mY;

        private float mVelocityX;
        private float mVelocityY;

        private int mWidth;
        private int mHeight;

        private Player player;

        public void scrollScreen()
        {
            if (!(player == null))
            {
                //mX = ((2 * player.Position.X + player.Type.Width) - 800) / 2;
                //mY = ((2 * player.Position.Y + player.Type.Height) - 640) / 2;
                mX = (player.Position.X + (player.Type.Width / 2.0f)) - (mWidth / 2.0f);
                mY = (player.Position.Y + (player.Type.Height / 2.0f)) - (mHeight / 2.0f);

                /* Let the midpoint of the viewport and Player be the same. If the viewport is such that X is in 
                 * the range of mX to mX+screenWidth and the Player is such that X is in the range of playerX to
                 * playerX + playerWidth, then
                 * 
                 * (2playerX+playerWidth)/2 = (2mX+width)/2
                 * And thus, mX = ((2playerX+playerWidth)-screenWidth)/2
                 */
            }
        }

        public void setPlayer(Player player)
        {
            this.player = player;
        }
        public WorldViewport(GraphicsDevice Device)
        {
            mWidth = Device.PresentationParameters.BackBufferWidth;
            mHeight = Device.PresentationParameters.BackBufferHeight;

            mX = 0;
            mY = 0;

            mVelocityX = 10;
            mVelocityY = 10;
        }

        public Vector2 WorldToViewportCoords(Vector2 WorldCoords)
        {
            WorldCoords.X = WorldCoords.X - mX;
            WorldCoords.Y = WorldCoords.Y - mY;
            return WorldCoords;
        }

        public Vector2 ViewportCoordsToWorldCoords(Vector2 ViewportCoords)
        {
            ViewportCoords.X = ViewportCoords.X + mX;
            ViewportCoords.Y = ViewportCoords.Y + mY;
            return ViewportCoords;
        }

        public List<Vector2> ViewportCoordsToWorldCoords(List<Vector2> ViewportCoords)
        {
            List<Vector2> temp = new List<Vector2>();

            foreach (Vector2 v in ViewportCoords)
            {
                temp.Add(ViewportCoordsToWorldCoords(v));
            }

            return temp;
        }

        public List<Vector2> WorldToViewportCoords(List<Vector2> ViewportCoords)
        {
            List<Vector2> temp = new List<Vector2>();

            foreach (Vector2 v in ViewportCoords)
            {
                temp.Add(WorldToViewportCoords(v));
            }

            return temp;
        }

        public List<Vector2> WorldToViewportAABB(AABB pass)
        {
            List<Vector2> toReturn = new List<Vector2>();
            Vertices temp = pass.Vertices;

            foreach (Vector2 v in temp)
            {
                Vector2 tV = WorldToViewportCoords(v);
                toReturn.Add(tV);
            }

            return toReturn;
        }

        public bool IsRectangleInViewport(Rectangle Rect)
        {
            if ((Rect.X <= (mX + mWidth)) &&
                ((Rect.X + Rect.Width) >= mX) &&
                (Rect.Y <= (mY + mHeight)) &&
                ((Rect.Y + Rect.Height) >= mY))
                return true;
            else
                return false;
        }

        public bool IsWorldPointInViewport(Vector2 point)
        {
            return (point.X >= mX && point.X <= (mX + mWidth)) &&
                (point.Y >= mY && point.Y <= (mY + mHeight));
        }

        public void MoveDown()
        {
            mY += mVelocityY;
        }

        public void MoveUp()
        {
            mY -= mVelocityY;
        }

        public void MoveRight()
        {
            mX += mVelocityX;
        }

        public void MoveLeft()
        {
            mX -= mVelocityX;
        }

        public float X
        {
            set { mX = value; }
            get { return mX; }
        }

        public float Y
        {
            set { mY = value; }
            get { return mY; }
        }

        public float VelocityX
        {
            set { mVelocityX = value; }
            get { return mVelocityX; }
        }

        public float VelocityY
        {
            set { mVelocityY = value; }
            get { return mVelocityY; }
        }

        public int Width
        {
            set { mWidth = value; }
            get { return mWidth; }
        }

        public int Height
        {
            set { mHeight = value; }
            get { return mHeight; }
        }
    }
}
